﻿using HandyControl.Controls;
using Supore.APP.UI.Controls.Notify;
using SuporeSliplampApp.Models;
using SuporeSliplampApp.Service;
using SuporeSliplampApp.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.IO;
using SuporeSliplampApp.Util;
using System.Web.UI.WebControls;


namespace SuporeSliplampApp.Views
{
    /// <summary>
    /// MainView.xaml 的交互逻辑
    /// </summary>
    public partial class MainView : UserControl
    {
        EditPatientService editPatientService;
        private string basePath = AppDomain.CurrentDomain.BaseDirectory;
        string currentid;
        public MainView()
        {
            InitializeComponent();
            var mainViewModel = MainViewModel.mainViewModel;
            this.DataContext = mainViewModel;
            editPatientService = new EditPatientService();
            MainViewModel.mainDelegate += MainViewModel_mainDelegate;
        }

        private void MainViewModel_mainDelegate(object sender, int e)
        {
            currentid = pIdTxt.Text;
        }


        private void SaveData()
        {           
            PatientModel patient = new PatientModel();
            patient.Name = nameTxt.Text;
            patient.P_ID = pIdTxt.Text;
            patient.Email = mailTxt.Text;
            patient.SEX = sexTxt.Text;
            patient.Adreess = adrTxt.Text;
            //patient.Creator = creatorTxt.Text;
            patient.Birth = datePicker.SelectedDate.Value;
            patient.PhoneNum = teleTxt.Text;
            if (!SaveCheck(patient))
                return;
            if (!editPatientService.EditPatient(patient, currentid))
            {
                if (Common.languageCode == "zh-CN")
                {
                    LayUINotify.ShowInfo("保存失败！");
                }
                else
                {
                    LayUINotify.ShowSuccess("Tip", "Save Successful");
                }
                return;
            }
            if (Common.languageCode == "zh-CN")
            {
                LayUINotify.ShowSuccess("修改成功！");
            }
            else
            {
                LayUINotify.ShowSuccess("Tip", "Modification Successful");
            }
            //LayUINotify.ShowSuccess("修改成功！");
            //editPanel.Height = 0;

            MainViewModel.GetInstance().patientModel.Name = patient.Name;
            MainViewModel.GetInstance().patientModel.P_ID = patient.P_ID;
            MainViewModel.GetInstance().patientModel.SEX = patient.SEX;
            MainViewModel.GetInstance().patientModel.Str_Birth = patient.Birth.ToShortDateString();
            MainViewModel.GetInstance().patientModel.Birth = patient.Birth;
            MainViewModel.GetInstance().patientModel.Adreess = patient.Adreess;
            MainViewModel.GetInstance().patientModel.PhoneNum = patient.PhoneNum;
            MainViewModel.GetInstance().patientModel.Email = patient.Email;
            MainViewModel.GetInstance().patientModel.Creator = patient.Creator;

        }

        private void DeleteData()
        {
            PatientModel patient = new PatientModel();
            patient.P_ID = pIdTxt.Text;
            if (!editPatientService.DeletePatient(patient))
            {
                if (Common.languageCode == "zh-CN")
                {
                    LayUINotify.ShowInfo("删除失败！");
                }
                else
                {
                    LayUINotify.ShowSuccess("Tip", "Deletion failed!");
                }
                //LayUINotify.ShowInfo("删除失败！");
                return;
            }
            if (Common.languageCode == "zh-CN")
            {
                LayUINotify.ShowSuccess("删除成功！");
            }
            else
            {
                LayUINotify.ShowSuccess("Tip", "Deletion successful!");
            }
            //LayUINotify.ShowSuccess("删除成功！");
            MainViewModel.GetInstance().patientModel = null;
        }

        private void DeletePatient()
        {
            string content;
            if (Common.languageCode == "zh-CN")
            {
                content = "确定删除当前病人吗？";
            }
            else
            {
                content = "Confirm delete current patient?";
            }
            //MessageBoxResult res = System.Windows.MessageBox.Show("确定删除当前病人吗?", "退出", MessageBoxButton.YesNo);

            MessageBoxResult res = MessageView.Show(content, "确认", MessageBoxButton.OKCancel);

            if (res != MessageBoxResult.OK)
            {
                return;
            }
            PatientModel patient = new PatientModel();
            patient.P_ID = pIdTxt.Text;
            if (!editPatientService.DeletePatient(patient))
            {
                if (Common.languageCode == "zh-CN")
                {
                    LayUINotify.ShowInfo("删除失败！");
                }
                else
                {
                    LayUINotify.ShowSuccess("Tip", "Deletion failed!");
                }
                //LayUINotify.ShowInfo("删除失败！");
                return;
            }
            if (Common.languageCode == "zh-CN")
            {
                LayUINotify.ShowSuccess("删除成功！");
            }
            else
            {
                LayUINotify.ShowSuccess("Tip", "Deletion successful!");
            }
            //LayUINotify.ShowSuccess("删除成功！");
            MainViewModel.GetInstance().patientModel.Name = "";
            MainViewModel.GetInstance().patientModel.P_ID = "";
            MainViewModel.GetInstance().patientModel.SEX = "";
            MainViewModel.GetInstance().patientModel.Str_Birth = "";
            sexTxt.SelectedIndex = -1;
            datePicker.Text = "";
            MainViewModel.GetInstance().patientModel.Adreess = "";
            MainViewModel.GetInstance().patientModel.PhoneNum = "";
            MainViewModel.GetInstance().patientModel.Email = "";
            MainViewModel.GetInstance().patientModel.Creator = "";
        }

        private bool SaveCheck(PatientModel p)
        {
            if (p.Name == string.Empty)
            {
                if (Common.languageCode == "zh-CN")
                {
                    LayUINotify.ShowInfo("姓名不能为空!");
                }
                else
                {
                    LayUINotify.ShowInfo("Tip", "Name is required!");
                }
                return false;
            }
            if (p.P_ID == string.Empty)
            {
                if (Common.languageCode == "zh-CN")
                {
                    LayUINotify.ShowInfo("病人ID不能为空！");
                }
                else
                {
                    LayUINotify.ShowInfo("Tip", "ID is required!");
                }
                return false;
            }
            if (p.SEX == string.Empty)
            {
                if (Common.languageCode == "zh-CN")
                {
                    LayUINotify.ShowInfo("性别不能为空！");
                }
                else
                {
                    LayUINotify.ShowInfo("Tip", "Gender is required!");
                }
                return false;
            }
            return true;
            
        }

        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            SaveData();
            MainViewModel.GetInstance().mainModel.ButtonWidth = 0;
            MainViewModel.GetInstance().mainModel.EditEnabled = false;

        }

        private void BtnDel_Click(object sender, RoutedEventArgs e)
        {
            DeletePatient();
            MainViewModel.GetInstance().mainModel.ButtonWidth = 0;
            MainViewModel.GetInstance().mainModel.EditEnabled = false;
        }

        private void quickBtn_Click(object sender, RoutedEventArgs e)
        {
            HomeViewModel.GetInstance().NavPage("SuporeSliplampApp.Views.CameraView");
        }

        private void NewPatinetBtn_Click(object sender, RoutedEventArgs e)
        {
            NewPatientView newPatientView = new NewPatientView();
            newPatientView.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            newPatientView.ShowDialog();
        }

        private void OpenPatientBtn_Click(object sender, RoutedEventArgs e)
        {
            OpenPatients openPatients = new OpenPatients();
            openPatients.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            openPatients.ShowDialog();
        }

        private void OperateGuide_Click(object sender, RoutedEventArgs e)
        {
            string pdf1path = System.IO.Path.Combine(basePath, "operateguide.pdf");

            if (File.Exists(pdf1path))
            {
                try
                {
                    Process.Start(new ProcessStartInfo(pdf1path) { UseShellExecute = true });
                }
                catch (Exception ex)
                {
                    if (Common.languageCode == "zh-CN")
                    {
                        System.Windows.MessageBox.Show("无法打开操作指南" + ex.Message);
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("Failed to open the quick guide" + ex.Message);
                    }
                    //System.Windows.MessageBox.Show("无法打开操作指南" + ex.Message);
                }
            }
            else 
            {
                if (Common.languageCode == "zh-CN")
                {
                    System.Windows.MessageBox.Show("未找到操作指南PDF文件");
                }
                else
                {
                    System.Windows.MessageBox.Show("Quick guide PDF file not found");
                }
                //System.Windows.MessageBox.Show("未找到操作指南PDF文件"); 
            }
        }

        private void UserManual_Click(object sender, RoutedEventArgs e)
        {
            string pdf1path = System.IO.Path.Combine(basePath, "usermanual.pdf");

            if (File.Exists(pdf1path))
            {
                try
                {
                    Process.Start(new ProcessStartInfo(pdf1path) { UseShellExecute = true });
                }
                catch (Exception ex)
                {
                    if (Common.languageCode == "zh-CN")
                    {
                        System.Windows.MessageBox.Show("无法打开用户手册" + ex.Message);
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("Failed to open the user manual" + ex.Message);
                    }
                    //System.Windows.MessageBox.Show("无法打开用户手册" + ex.Message);
                }
            }
            else
            {
                if (Common.languageCode == "zh-CN")
                {
                    System.Windows.MessageBox.Show("未找到用户手册PDF文件");
                }
                else
                {
                    System.Windows.MessageBox.Show("User manual PDF file not found");
                }
                //System.Windows.MessageBox.Show("未找到用户手册PDF文件");
            }
        }

        private void OpenVerInfo_Click(object sender, RoutedEventArgs e)
        {
            VerInfoView verinfo = new VerInfoView();
            var parentWindow = GetParentWindow(this);
            verinfo.Owner = parentWindow;
            //verinfo.WindowStartupLocation = WindowStartupLocation.CenterOwner;

            verinfo.Top = (1080 - verinfo.Height) / 2;
            verinfo.Left = (1920 - verinfo.Width) / 2;

            verinfo.ShowDialog();
        }

        private static System.Windows.Window GetParentWindow(DependencyObject child)
        {
            DependencyObject parent = VisualTreeHelper.GetParent(child);

            if (parent == null) return null;

            if (parent is System.Windows.Window) return (System.Windows.Window)parent;
            else return GetParentWindow(parent);
        }


    }
}
